module(...,package.seeall)
-----------------------------------------------------------------------------------------
--
-- tutorial.lua
--
-----------------------------------------------------------------------------------------

local storyboard = require( "storyboard" )
local scene = storyboard.newScene()

-- include Corona's "widget" library
local widget = require "widget"
local audioGame = require "audioGame";
local gameConstant = require "GameConstant";
--------------------------------------------
local trackLeft
local trackRight
local bloodLeft
local bloodRight
local background
local titleLogo
local playBtn
local timeMoveButton = 200
local timeMoveToButton = 200
local i = 0
local statusTrack
local statusBlood
local trackText
local bloodText

-- forward declarations and other locals
local listenerplayBtn = function( obj )
            transition.to( playBtn, { time = timeMoveToButton, alpha=1.0,x = display.contentWidth*0.5, y=5*display.contentHeight/6} )
        end

-- 'onRelease' event listener for playBtn
local function onplayBtnRelease( )
	playBtn.xScale = 1;
        playBtn.yScale = 1;
        --storyboard.removeScene( "mapPlay" );
        local menu = storyboard.loadScene( "menu" )
        -- Later, transition to the scene (no loading necessary)
        if trackLeft then trackLeft:removeSelf( ); trackLeft = nil end 
        if trackRight then trackRight:removeSelf( ); trackRight = nil end 
        if bloodLeft then bloodLeft:removeSelf( ); bloodLeft = nil end 
        if bloodRight then bloodRight:removeSelf( ); bloodRight = nil end
        --if trackText then trackText.setText("") end
        --if bloodText then bloodText.setText("")  end
        local playGame = storyboard.loadScene( "playGame" )
	storyboard.gotoScene( "playGame", "fade", 100 )
	return true	-- indicates successful touch
    end
-- "OnPress" event listener for playBtn
local function onplayBtnPress()
    audio.stop(audioGame.statusBackground);
    if gameConstant.statusSound then
        audio.play(audioGame.finishAudio);
    end
    playBtn.xScale = 1.5;playBtn.yScale = 1.5;
    return true;
end 

-- forward declarations and other locals
local listenertrackLeft = function( obj )
            transition.to( trackLeft, { time = timeMoveToButton, alpha=1.0,x = display.contentWidth/3-20, y=display.contentHeight/3} )
        end

-- 'onRelease' event listener for trackLeft
local function trackLeftRelease( )
	trackLeft.xScale = 1;
        trackLeft.yScale = 1;
        if statusTrack == 0 then statusTrack = 11 else 
            statusTrack = statusTrack - 1
        end
	return true	-- indicates successful touch
    end
-- "OnPress" event listener for trackLeft
local function trackLeftPress()
    audio.play( audioGame.buttonClick  )
    trackLeft.xScale = 1.5;trackLeft.yScale = 1.5;
    return true;
end  


-- forward declarations and other locals
local listenertrackRight = function( obj )
            transition.to( trackRight, { time = timeMoveToButton, alpha=1.0,x = 2*display.contentWidth/3+10, y=display.contentHeight/3} )
        end

-- 'onRelease' event listener for trackRight
local function trackRightRelease( )
	trackRight.xScale = 1;
        trackRight.yScale = 1;
        if statusTrack == 11 then statusTrack = 0 else 
            statusTrack = statusTrack + 1
        end
        
	return true	-- indicates successful touch
    end
-- "OnPress" event listener for trackRight
local function trackRightPress()
    audio.play( audioGame.buttonClick  )
    trackRight.xScale = 1.5;trackRight.yScale = 1.5;
    return true;
end 


-- forward declarations and other locals
local listenerbloodLeft = function( obj )
            transition.to( bloodLeft, { time = timeMoveToButton, alpha=1.0,x = display.contentWidth/3-20, y=display.contentHeight/3+50} )
        end

-- 'onRelease' event listener for bloodLeft
local function bloodLeftRelease( )
	bloodLeft.xScale = 1;
        bloodLeft.yScale = 1;
        if statusBlood then statusBlood = false
        else statusBlood = true end
        
	return true	-- indicates successful touch
    end
-- "OnPress" event listener for bloodLeft
local function bloodLeftPress()
    audio.play( audioGame.buttonClick  )
    bloodLeft.xScale = 1.5;bloodLeft.yScale = 1.5;
    return true;
end 


-- forward declarations and other locals
local listenerBloodRight = function( obj )
            transition.to( bloodRight, { time = timeMoveToButton, alpha=1.0,x = 2*display.contentWidth/3+10, y=display.contentHeight/3+50} )
        end

-- 'onRelease' event listener for bloodRight
local function bloodRightRelease( )
	bloodRight.xScale = 1;
        bloodRight.yScale = 1;
        if statusBlood then statusBlood = false
        else statusBlood = true end
	return true	-- indicates successful touch
    end
-- "OnPress" event listener for bloodRight
local function bloodRightPress()
    audio.play( audioGame.buttonClick  )
    bloodRight.xScale = 1.5;bloodRight.yScale = 1.5;
    return true;
end 

-- Move button 

local listener2 = function (obj) 
end

-- move object in frame

local function move(event)
    if statusTrack == 0 then 
       trackText:setText("Random") 
    elseif statusTrack == 1 then  trackText:setText("Training ") 
    elseif statusTrack == 2 then  trackText:setText("Track 1")
    elseif statusTrack == 3 then  trackText:setText("Track 2")
    elseif statusTrack == 4 then  trackText:setText("Track 3")
    elseif statusTrack == 5 then  trackText:setText("Track 4")
    elseif statusTrack == 6 then  trackText:setText("Track 5")
    elseif statusTrack == 7 then  trackText:setText("Track 6")
    elseif statusTrack == 8 then  trackText:setText("Track 7")
    elseif statusTrack == 9 then  trackText:setText("Track 8")
    elseif statusTrack == 10 then  trackText:setText("Track 9")
    elseif statusTrack == 11 then  trackText:setText("Track 10")
    end
    if statusBlood then bloodText:setText("Blood Enabled ") else bloodText:setText("Blood Disabled") end
    
    
end
-----------------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
-- 
-- NOTE: Code outside of listener functions (below) will only be executed once,
--		 unless storyboard.removeScene() is called.
-- 
-----------------------------------------------------------------------------------------

-- Called when the scene's view does not exist:
function scene:createScene( event )
	local group = self.view
        --display background image
        local background = display.newImageRect( "images/menu/background.png", display.contentWidth, display.contentHeight )
	background:setReferencePoint( display.TopLeftReferencePoint )
	background.x, background.y = 0, 0
        
        -- create title logo option
        local titleLogo = display.newImage( "images/menu/lr_logo.png")
        titleLogo:setReferencePoint( display.CenterReferencePoint )
        titleLogo.x = display.contentWidth/2
	titleLogo.y = 40
        
        
        -- create text sound
        trackText = display.newText("Random", display.contentWidth/3+40, display.contentHeight/3-20, native.systemFont, 16 )
        trackText:setTextColor(0, 0, 0)
        bloodText= display.newText("Blood Enabled", display.contentWidth/3+20, display.contentHeight/3+30, native.systemFont, 16 )
        bloodText:setTextColor(0, 0, 0)
        
        group:insert( background )
        group:insert( titleLogo )
        group:insert( trackText )
        group:insert( bloodText )
end

-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
       local group = self.view;
       statusTrack = 0;
       statusBlood = false;
       playBtn  = widget.newButton{
		labelColor = { default={255}, over={128} },
		default = "images/menu/lr_menu_start.png",
		width = 150, height = 30,
                onPress = onplayBtnPress,
		onRelease = onplayBtnRelease -- event listener function
	}
	playBtn:setReferencePoint( display.CenterReferencePoint )
	playBtn.x = display.contentWidth*0.5
	playBtn.y = 5*display.contentHeight/6+30
        
	-- all display objects must be inserted into group
	transition.from( playBtn, { time = timeMoveButton,  x=display.contentWidth/2, y=0, onComplete=listenerplayBtn } )
        
        -- create trackLeft images
        trackLeft = widget.newButton{
		default="images/menu/lr_button_left.png",
                onPress = trackLeftPress,
		onRelease = trackLeftRelease	-- event listener function
	}
        trackLeft:setReferencePoint( display.CenterReferencePoint )
	trackLeft.x = display.contentWidth/3-10
	trackLeft.y = display.contentHeight/3
        transition.from( trackLeft, { time = timeMoveButton,  x = 0, y = display.contentHeight/3,
                            onComplete=listenertrackLeft } )
	
        -- create trackRight images
        trackRight = widget.newButton{
		default="images/menu/lr_button_right.png",
                onPress = trackRightPress,
		onRelease = trackRightRelease	-- event listener function
	}
        trackRight:setReferencePoint( display.CenterReferencePoint )
	trackRight.x = 2*display.contentWidth/3
	trackRight.y = display.contentHeight/3
        transition.from( trackRight, { time = timeMoveButton,  x = display.contentWidth, y = display.contentHeight/3,
                            onComplete=listenertrackRight })
        -- create bloodLeft images
       bloodLeft = widget.newButton{
		default="images/menu/lr_button_left.png",
                onPress = bloodLeftPress,
		onRelease = bloodLeftRelease	-- event listener function
	}
        bloodLeft:setReferencePoint( display.CenterReferencePoint )
	bloodLeft.x = display.contentWidth/3-10
	bloodLeft.y = display.contentHeight/3 + 50
        transition.from( bloodLeft, { time = timeMoveButton,  x = 0, y = display.contentHeight/3+50,
                            onComplete=listenerbloodLeft } )
        -- create bloodRight images
        bloodRight = widget.newButton{
		default="images/menu/lr_button_right.png",
                onPress = bloodRightPress,
		onRelease = bloodRightRelease	-- event listener function
	}
        bloodRight:setReferencePoint( display.CenterReferencePoint )
	bloodRight.x = 2*display.contentWidth/3
	bloodRight.y = display.contentHeight/3 + 50
        transition.from( bloodRight, { time = timeMoveButton,  x = display.contentWidth, y = display.contentHeight/3+50,
                            onComplete=listenerBloodRight } )
        -- print("EnterScene");
        
        group:insert( trackLeft )
        group:insert( trackRight )
        group:insert( bloodLeft )
        group:insert( bloodRight )
        group:insert( playBtn )
        
end

-- Called when scene is about to move offscreen:
function scene:exitScene( event )
	local group = self.view
        if playBtn then 
           playBtn:removeSelf()
           playBtn = nil
          -- print ("remove playBtn")
        end
	-- INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)
	
end

-- If scene's view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
	local group = self.view
        --playBtn:removeSelf()	-- widgets must be manually removed
        --storyboard:removeSelf("menu")
        --playBtn = nil
        --print("Xoa playBtn");                                                         
end

-----------------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
-----------------------------------------------------------------------------------------

-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )

-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )

-- "exitScene" event is dispatched whenever before next scene's transition begins
scene:addEventListener( "exitScene", scene )

-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )

-----------------------------------------------------------------------------------------
Runtime:addEventListener( "enterFrame", move );
return scene